$m(function() {
    var that = this;
    var $this = this.element;

    var page = $("#tempo-casebase-page");
    var grid = $("#tempo-mine-casebase-grid");
    var gridSelf = $("#tempo-mine-casebase-grid1");
    var index=0;
    /**
     * 初始化
     */
    var init = function(){
        initLayout();
        /**
         * 初始化tabs 默认先展示公共案例
         */
        $("#tabs_cloud_casebase_sys_config").tabs({
            activate: function( event, ui ) {
                //获取tabs的id
                tabsId = ui.newPanel.attr('id')
                if(tabsId === "casebase-config-public") {
                    //去初始化公共库
                    renderGrid();
                }
                if(tabsId === "casebase-config-self") {
                    //去初始化私人库
                    renderSelfGrid();
                }
            }
        });
        renderGrid();
    };
    /**
     * 初始化布局
     */
    var initLayout = function(){
        page.layout({
            north : {
                resizable : false,
                size : 45,
                spacing_open : 0,
                closable : false
            },
            center:{
                onresize:TempoUtils.throttle(function(){
                    grid.datagrid("resize");
                },200,true)
            }
        });
    };

    /**
     * 判断发布区该应用下还有没有流程  没有返回false
     * @param appId
     * @returns {boolean}
     */
    var publishFlowIsNull = function (appId) {
        var flag = false;
        $.ajax({
            url: contextPath + '/api/ai/resource/template/flow-publish-info',
            method: 'POST',
            async:false,
            data:{
                appId:appId,
            },
            success: function(rs) {
                if(rs.data){
                    flag = rs.data;
                }
            }

        })
        return flag;
    }

    /**
     * 搜索框事件处理
     */
    var searchHandle = function(text){
        grid.datagrid("reloadGrid",{
            text :　text
        },false);
    }

    /**
     * 搜索框事件处理(私人)
     */
    var searchHandles = function(text){
        gridSelf.datagrid("reloadGrid",{
            text :　text
        },false);
    }

    /**
     * 创建工具栏的配置数组
     * */
    var createToolBarConfig = function () {
        var arr = [['新增','add','',addCaseBase]];
        var tbConfig = [];
        $.each(arr, function(i, item) {
            tbConfig.push({
                id : "sample_btn_" + item[1],
                text : item[0],
                icon : item[2],
                click : item[3]
            });
        });
        return tbConfig;
    }
    /**
     * 修改火狐浏览器下 案例高度显示问题
     * 具体问题不是很清楚 本地调整高度都正常时  提交到58 谷歌IE都正常  火狐显示不全
     * 目前对照着58的火狐进行修改的高度 就很奇怪！！！！
     * @param grid
     */
    var changeGridHeight = function (grid) {
        var caseBaseDom = $("#content");
        caseBaseDom.find("#tabs_cloud_casebase_config").css("height", "100%");
        //获取当前分辨率的宽度
        var width = screen.width;
        if (TempoUtils.isFirefox()) {
            if (width === 1366) {
                caseBaseDom.find("#gview_tempo-mine-casebase-grid").css("height", "83%");
                caseBaseDom.find("#gview_tempo-mine-casebase-grid1").css("height", "83%");
            } else {
                caseBaseDom.find("#gview_tempo-mine-casebase-grid").css("height", "89%");
                caseBaseDom.find("#gview_tempo-mine-casebase-grid1").css("height", "89%");
            }
            grid.parent().parent().css("height", "calc(100% - 100px)");
        } else {
            caseBaseDom.find("#gview_tempo-mine-casebase-grid").css("height", "96%");
            caseBaseDom.find("#gview_tempo-mine-casebase-grid1").css("height", "96%");
            grid.parent().parent().css("height", "calc(100% - 100px)");
        }
    }
    /**
     * 绘制列表(公共)
     */
    var renderGrid = function () {
        var heights = $(".tempo-casebase-center").height();
        var widths = $(".tempo-casebase-center").width();
        grid.datagrid({
            url: contextPath + "/api/ai/sysconfig/casebase/list-template",
            datatype: 'json',
            fit: false,
            height: heights-120,
            width: widths,
            rownumbers: true,
            separator: false,
            loadui: 'disable',
            colNames: [
                'path', 'pdfFile', 'tappFile', 'pngFile',
                '名称',
                '作者',
                '技术',
                '描述',
                '创建时间',
                '操作',
            ],
            toolbar: createToolBarConfig(),
            colModel: [{
                name: 'path',
                sortable: false,
                hidden: true
            }, {
                name: 'pdfFile',
                sortable: false,
                hidden: true
            }, {
                name: 'tappFile',
                sortable: false,
                hidden: true
            }, {
                name: 'pngFile',
                sortable: false,
                hidden: true
            }, {
                name: 'name',
                sortable: false,
                align: 'center',
                formatter : function(obj) {
                    return TempoMineUtils.encodeHTML(obj);
                }
            }, {
                name: 'author',
                sortable: false,
                align: 'center'
            }, {
                name: 'category',
                sortable: false,
                align: 'center'
            }, {
                name: 'description',
                sortable: false,
                align: 'center',
                formatter : function(obj) {
                    return TempoMineUtils.encodeHTML(obj);
                }
            }, {
                name: 'creatTime',
                sortable: false,
                align: 'center',
                formatter: function (obj, grid, rows, state) {
                    if (obj) {
                        return DateUtils.formatDate(new Date(obj), "yyyy-MM-dd HH:mm:ss");
                    }
                    return;
                }
            }, {
                name: 'action',
                sortable: false,
                buttons: [{
                    id: "edit",
                    title: "编辑",
                    icon: "ui-icon tempo-ui-icon tempo-ui-icon-edit",
                    click: function (e, et) {
                        updateQueue("", "编辑", et.rowData);
                    }
                }, {
                    id: "delete",
                    title: '删除',
                    icon: "ui-icon tempo-ui-icon tempo-ui-icon-trash",
                    click: function (e, dt) {
                        removeCasebase(dt.rowData);
                    }
                }]
            }],
            //分页
            pagination: true,
            postData: {
                text: ""
            },
            //搜索框
            searchBar: true,
            searchBarOptions: {
                useRadius:false,
                placeholder: '请输入名称、技术或作者',
                width: 210,
                onSearch: function (text) {
                    searchHandle($.trim(text));
                }
            },
        });
        //修改火狐下的样式
        changeGridHeight(grid);
    }

    /**
     *删除案例(公共)
     */
    function removeCasebase(data){
        var deleteDialog = $('<div style="width:100%;height:100%;background-color: #ffffff"/></div>').appendTo($('body'));
        deleteDialog.dialog({
            modal : true,
            width : 420,
            height : 118,
            title : TempoUtils.locale("mine.prompt"),
            resizable : false,
            closeOnEscape : true,
            close:function(){
                deleteDialog.remove();
            },
            open : function(){
                $('<div  class="tempo-sys-delete-dialog-content">'+TempoUtils.locale("mine.shanchubukehufu")+'</div>').appendTo($(deleteDialog));
            },
            buttons:[{
                text:TempoUtils.locale("mine.publish-dialog-sure"),
                id: 'tempo-sys-ok-btn',
                click:function(){
                    var process = $("<div/>").tempoprogressbar();
                    $.ajax({
                        url:contextPath+"/api/ai/sysconfig/casebase/deleteCaseBase",
                        data:{
                            fileName: data.path,
                            name: data.name,
                        },
                        type:'post',
                        success:function(result){
                            process.tempoprogressbar('destroy');
                            deleteDialog.dialog("close");
                            if(result){
                                grid.trigger("reloadGrid");
                            }else{
                                $("<div/>").alert({
                                    title: TempoUtils.locale("mine.tooltip"),
                                    msg: TempoUtils.locale("sysheader.shanchushibai"),
                                });
                            }
                        }
                    });
                }
            },{
                text:TempoUtils.locale("mine.publish-dialog-cancel"),
                id: 'tempo-sys-cancel-btn',
                click:function(){
                    deleteDialog.dialog("close");
                }
            }]
        })
    }

    /**
     * 绘制列表(私人)
     */
    var renderSelfGrid = function () {
        var heights = $(".tempo-casebase-center1").height();
        var widths = $(".tempo-casebase-center1").width();
        gridSelf.datagrid({
            url: contextPath + "/api/ai/sysconfig/casebase/list-template-self",
            datatype: 'json',
            fit: false,
            height: heights-120,
            width: widths,
            rownumbers: true,
            separator: false,
            loadui: 'disable',
            colNames: [
                'path',
                'pdfFile',
                'tappFile',
                'pngFile',
                '名称',
                '作者',
                '技术',
                '描述',
                '创建时间',
                '操作',
            ],
            colModel: [{
                name: 'path',
                sortable: false,
                hidden: true
            }, {
                name: 'pdfFile',
                sortable: false,
                hidden: true
            }, {
                name: 'tappFile',
                sortable: false,
                hidden: true
            }, {
                name: 'pngFile',
                sortable: false,
                hidden: true
            }, {
                name: 'name',
                sortable: false,
                align: 'center',
                formatter : function(obj) {
                    return TempoMineUtils.encodeHTML(obj);
                }
            }, {
                name: 'author',
                sortable: false,
                align: 'center'
            }, {
                name: 'category',
                sortable: false,
                align: 'center'
            }, {
                name: 'description',
                sortable: false,
                align: 'center',
                formatter : function(obj) {
                    return TempoMineUtils.encodeHTML(obj);
                }
            }, {
                name: 'creatTime',
                sortable: false,
                align: 'center',
                formatter: function (obj, grid, rows, state) {
                    if (obj) {
                        return DateUtils.formatDate(new Date(obj), "yyyy-MM-dd HH:mm:ss");
                    }
                    return;
                }
            }, {
                name: 'action',
                sortable: false,
                buttons: [{
                    id: "edit",
                    title: "编辑",
                    icon: "ui-icon tempo-ui-icon tempo-ui-icon-edit",
                    click: function (e, et) {
                        var data = et.rowData;
                        $.ajax({
                            url: contextPath + '/api/ai/resource/template/get',
                            method: 'GET',
                            async: false,
                            data: {
                                id: data.fileName,
                            },
                            success: function(result) {
                                var res = result.data;
                                //案例应用被删除的情况下 res为null 无法编辑  也没有提示信息的问题
                                if(res != null){
                                    var appId = res.appId;
                                    data.appId = appId;
                                    if (publishFlowIsNull(data.appId)) {
                                        updateSelf("", "编辑", data);
                                    }else{
                                        $("<div/>").alert({
                                            width: 500,
                                            height: 197,
                                            msg: "案例应用已被取消发布，无法编辑，请重新发布应用！",
                                        });
                                    }
                                }else {
                                    $("<div/>").alert({
                                        width: 500,
                                        height: 197,
                                        msg: "该案例应用已被删除，无法继续编辑！",
                                    });
                                }
                            }
                        })

                    }
                }, {
                    id: "delete",
                    title: '删除',
                    icon: "ui-icon tempo-ui-icon tempo-ui-icon-trash",
                    click: function (e, dt) {
                        removeTemplate(dt.rowData);
                        gridSelf.trigger("reloadGrid");
                    }
                }]
            }],
            //分页
            pagination: true,
            postData: {
                text: ""
            },
            //搜索框
            searchBar: true,
            searchBarOptions: {
                useRadius:false,
                placeholder: '请输入名称、技术或作者',
                width: 210,
                onSearch: function (text) {
                    searchHandles($.trim(text));
                }
            },
        });
        //修改火狐下的样式
        changeGridHeight(gridSelf);
    }



    /**
     * 新增弹框
     */
    var addCaseBase = function (event, data){
        var dialog = $("<div/>", {
            'class' : 'tempo-casebase-dialog',
        });
        dialog.dialog({
            modal : true,
            width : 510,
            height : 480,
            resizable : false,
            title : "新增案例模板",
            open:function(){
                $m.loadHtml(contextPath+"/api/ai/sysconfig/casebase/view", dialog, "");
            },
            buttons : [ {
                text : '保存',
                id: 'tempo-casebase-add-btn',
                click : function() {
                    var pro = $("<div/>").tempoprogressbar();
                    var $dom = $('#dialog-casebase-tempo-public').find('form');
                    //获取表单元素
                    var record = TempoUtils.getRecord($dom);
                    //回填渲染插件
                    TempoUtils.parserUi($dom, {});
                    //表单元素验证
                    var success = $dom.formValidate();
                    if(!success){
                        pro.tempoprogressbar("destroy");
                        return;
                    }
                    $dom.ajaxSubmit({
                        dataType:'json',
                        type : 'post',
                        beforeSubmit:function(result){
                            var flage = true;
                            var pdfName = result[5].value.name;
                            $.each($("input[type=file]"),function(n,m){
                                var vule = m.value;
                                if(vule == ""){
                                    $(this.parentElement).addClass("addBoxRed");
                                }else{
                                    $(this.parentElement).removeClass("addBoxRed");
                                }
                                if(!vule){
                                    flage = false;
                                }
                            });
                            if(flage == false){
                                $("#fileMsg").css("display","block").text("附件文件不能为空!");
                            }else{
                                $("#fileMsg").css("display","none");
                            }
                            if(pdfName && pdfName.length > 60){
                                $("#fileMsg").css("display","block").text("附件名称最大长度60,请修改!");
                                flage = false;
                            }
                            if(!flage){
                                pro.tempoprogressbar("destroy");
                            }
                            return flage;
                        },
                        success:function(rs){
                            pro.tempoprogressbar("destroy");
                            if(rs.success){
                                grid.datagrid("reloadGrid");
                                dialog.dialog("destroy").remove();
                            }else{
                                $("<div/>").alert({
                                    msg : rs.message,
                                    title : '提示'
                                });
                            }
                        },
                        error:function(){
                            pro.tempoprogressbar("destroy");
                            dialog.dialog("destroy").remove();
                        }
                    });
                }
            },{
                text : '取消',
                id: 'tempo-casebase-reset-btn',
                click : function() {
                    dialog.dialog("destroy").remove();
                }
            } ],
            close : function() {
                dialog.dialog("destroy").remove();	// 销毁弹出框,并返回
            }
        });

    };

    /**
     *删除案例
     */
    var removeTemplate = function(data){
        var Dialog = $('<div style="width:100%;height:100%;background-color: #ffffff"/></div>').appendTo($('body'));
        Dialog.dialog({
            modal : true,
            width : 420,
            height : 118,
            title : TempoUtils.locale("mine.prompt"),
            resizable : false,
            closeOnEscape : true,
            close:function(){
                Dialog.remove();
            },
            open : function(){
                $('<div  class="tempo-sys-delete-dialog-content">'+TempoUtils.locale("mine.shanchubukehufu")+'</div>').appendTo($(Dialog));
            },
            buttons:[{
                text:TempoUtils.locale("mine.publish-dialog-sure"),
                id: 'tempo-sys-ok-btn',
                click:function(){
                    var process = $("<div/>").tempoprogressbar();
                    $.ajax({
                        url:contextPath+"/api/ai/sysconfig/casebase/delete-self-template",
                        data:{
                            filePath: data.path,
                            code: data.fileName,
                        },
                        type:'post',
                        success:function(result){
                            process.tempoprogressbar('destroy');
                            Dialog.dialog("close");
                            if(result){
                                gridSelf.trigger("reloadGrid");
                            }else{
                                $("<div/>").alert({
                                    title: TempoUtils.locale("mine.tooltip"),
                                    msg: TempoUtils.locale("sysheader.shanchushibai"),
                                });
                            }
                        }
                    });
                }
            },{
                text:TempoUtils.locale("mine.publish-dialog-cancel"),
                id: 'tempo-sys-cancel-btn',
                click:function(){
                    Dialog.dialog("close");
                }
            }]
        })
    }

    /**
     * 修改案例库内容
     */
    var  updateQueue = function(event, textTitle, data){
        var dialog = $("<div/>", {
            'class' : 'tempo-sample-code-dialog'
        });
        dialog.dialog({
            modal : true,
            width : 510,
            height : 480,
            resizable : false,
            title : '编辑案例模板',
            open:function(){
                $m.loadHtml(contextPath+"/api/ai/sysconfig/casebase/view", dialog, "",data);
            },
            buttons : [ {
                text : '保存',
                id: 'tempo-casebase-add-btn',
                click : function() {
                    var pro = $("<div/>").tempoprogressbar();
                    var $dom = $('#dialog-casebase-tempo-public').find('form');
                    var record = TempoUtils.getRecord($dom);
                    TempoUtils.parserUi($dom, {});
                    var success = $dom.formValidate();
                    if(!success){
                        pro.tempoprogressbar("destroy");
                        return;
                    }
                    $dom.ajaxSubmit({
                        dataType:'json',
                        type : 'post',
                        beforeSubmit:function(result){
                            var obj = {};
                            obj.name = "category";
                            obj.value = $("#category").val();
                            obj.type = "select";
                            obj.required = false;
                            result.push(obj);
                            var obj1 = {};
                            obj1.name = "oldName";
                            obj1.value = data.name;
                            obj1.type = "input";
                            obj1.required = false;
                            result.push(obj1);
                            var flage = true;
                            $.each($("input[type=file]"),function(n,m){
                                var vule = m.value;
                                if(!vule){
                                    $("#fileMsg").css("display","block");
                                    $(this.parentElement).addClass("addBoxRed");
                                }else{
                                    $("#fileMsg").css("display","none");
                                    $(this.parentElement).removeClass("addBoxRed");
                                }
                            });
                            $.each($(".uploadFile-btn"),function(j,k){
                                if($(this).css("display")=="block"){
                                    $("#fileMsg").text("附件文件不能为空!");
                                    flage = false;
                                }
                            });
                            if(!flage){
                                pro.tempoprogressbar("destroy");
                            }
                            return flage;
                        },
                        success:function(rs){
                            pro.tempoprogressbar("destroy");
                            if(rs.success){
                                grid.datagrid("reloadGrid");
                                dialog.dialog("destroy").remove();
                            }else{
                                $("<div/>").alert({
                                    msg : rs.message,
                                    title : '提示'
                                });
                            }
                        },
                        error:function(){
                            pro.tempoprogressbar("destroy");
                            dialog.dialog("destroy").remove();
                        }
                    });
                }
            },{
                text : '取消',
                click : function() {
                    dialog.dialog("destroy").remove();
                }
            } ],
            close : function() {
                dialog.dialog("destroy").remove();	// 销毁弹出框,并返回
            }
        });
    };
    /**
     * 提示信息框
     * @param dialog
     */
    var themeTooltip = function (dialog) {
        var iconDiv = $("<div id='tooltip-icon' class='ui-icon tempo-ui-icon tempo-ui-icon-tip tempo-mine-button' style='margin-right: 35px;float: left;margin-top: 13px;cursor:pointer;'>");
        var titleDiv = $(dialog.prev().children()[0]).css("width","60px");
        iconDiv.insertAfter(titleDiv);
        var tooltip = "<div id='theme-tooltip' style='background: #ffffff;" +
            "position:absolute;position:absolute;width:468px;height:40px;backgroud:#EAEDF1;" +
            "box-shadow:0px 0px 10px rgba(46,49,53,0.44);z-index:99999;text-align:left;padding: 6px 6px 6px 6px'>" +
            "<div>" + TempoUtils.locale("mine.theme-tip") + "</div>"+
            "<div>" + TempoUtils.locale("mine.tooltips") + "</div>";
        //判断只初始化一次
        if ($("#theme-tooltip").length == 0) {
            $('body').append(tooltip);
            $("#theme-tooltip").hide();
        }
        $("#tooltip-icon").mouseover(function(){
            var x = $("#tooltip-icon").offset().left;
            var y = $("#tooltip-icon").offset().top;
            $("#theme-tooltip").css("left",x+20);
            $("#theme-tooltip").css("top",y+20);
            $("#theme-tooltip").show();
        })
        $("#tooltip-icon").mouseout(function(){
            $("#theme-tooltip").hide();
        })
    }

    /**
     * 修改案例库内容(私人1)
     */
    var  updateSelf = function(event, textTitle, data){
        var dialog = $("<div/>", {
            'class' : 'tempo-sample-code-dialog—self'
        });
        dialog.dialog({
            modal : true,
            width : 760,
            height : 580,
            resizable : false,
            title : '编辑案例',
            open:function(){
                themeTooltip(dialog);
                $m.loadHtml(contextPath+"/api/ai/sysconfig/casebase/view/pub-self", dialog, "",data);
                $("#tempo-casebase-add-btn").hide();
                $("#tempo-casebase-up-btn").hide();
            },
            buttons : [{
                text: "下一步",
                id: 'tempo-casebase-next-btn',
                click: function () {
                    var $dom = $('#dialog-casebase-tempo').find('form');
                    $dom.find("input[name='appId']").val(data.appId);
                    //获取表单元素
                    var record = TempoUtils.getRecord($dom);
                    //回填渲染插件
                    TempoUtils.parserUi($dom, {});
                    //表单元素验证
                    var success = $dom.formValidate();
                    if(!success){
                        return;
                    }
                    //判断上传文件是否为空加颜色
                    $.each($("input[type=file]"),function(n,m){
                        var vule = m.value;
                        if(vule == ""){
                            $(this.parentElement).addClass("addBoxRed");
                        }else{
                            $(this.parentElement).removeClass("addBoxRed");
                        }
                        if(!vule){
                            flage = false;
                        }
                    });
                    var flage = true;
                    $.each(dialog.find(".fimeName-text span"),function(n,m){
                        var vule = $(this).text();
                        if(!vule){
                            flage = false;
                        }
                    });
                    if(flage == false){
                        $("#fileMsg").css("display","block").text("附件文件不能为空!");
                        $("#fileMsg").css('margin-left','32%');
                        $("#fileMsg").css('margin-bottom','14px');
                    }else{
                        $("#fileMsg").css("display","none");
                    }
                    if(dialog.find("#pdfSpan").text().length > 60){
                        $("#fileMsg").css("display","block").text("附件名称最大长度60,请修改!");
                        flage = false;
                    }
                    if(flage){
                        index = 1;
                        ControlContent(index,dialog);
                        $("#tempo-casebase-next-btn").hide();
                        $("#tempo-casebase-up-btn").show();
                        $("#tempo-casebase-add-btn").show();
                        Eventbus.triggerHandler("initEvent.mine");
                        Eventbus.triggerHandler("renderPage.mine");
                    }
                    return flage;
                }
            },{
                text : '上一步',
                id: 'tempo-casebase-up-btn',
                click : function() {
                    index = 0;
                    ControlContent(index,dialog);
                    $("#tempo-casebase-next-btn").show();
                    $("#tempo-casebase-up-btn").hide();
                    $("#tempo-casebase-add-btn").hide();
                }
            },{
                text : '保存',
                id: 'tempo-casebase-add-btn',
                click : function() {
                    var pro = $("<div/>").tempoprogressbar();
                    var $dom = $('#dialog-casebase-tempo').find('form');
                    //获取权限勾选的权限数据
                    var builPermissionDatas = Eventbus.triggerHandler("builPermissionData.mine")[0];
                    var record = TempoUtils.getRecord($dom);
                    TempoUtils.parserUi($dom, {});
                    var success = $dom.formValidate();
                    if(!success){
                        pro.tempoprogressbar("destroy");
                        return;
                    }
                    $dom.ajaxSubmit({
                        dataType:'json',
                        type : 'post',
                        beforeSubmit:function(result){
                            var obj = {};
                            obj.name = "category";
                            obj.value = $("#category").val();
                            obj.type = "select";
                            obj.required = false;
                            result.push(obj);
                            var obj1 = {};
                            obj1.name = "oldName";
                            obj1.value = data.name;
                            obj1.type = "input";
                            obj1.required = false;
                            result.push(obj1);
                            var flage = true;
                            $.each($("input[type=file]"),function(n,m){
                                var vule = m.value;
                                if(!vule){
                                    $("#fileMsg").css("display","block");
                                    $(this.parentElement).addClass("addBoxRed");
                                }else{
                                    $("#fileMsg").css("display","none");
                                    $(this.parentElement).removeClass("addBoxRed");
                                }
                            });
                            $.each($(".uploadFile-btn"),function(j,k){
                                if($(this).css("display")=="block"){
                                    $("#fileMsg").text("附件文件不能为空!");
                                    flage = false;
                                }
                            });
                            if(!flage){
                                pro.tempoprogressbar("destroy");
                            }
                            return flage;
                        },
                        //编辑案例时添加生成案例中 请稍后....
                        beforeSend: function() {
                            $("<div/>").alert({
                                width: 500,
                                height: 197,
                                msg: "正在生成案例中，请稍后...",
                            });
                        },
                        success:function(rs){
                            pro.tempoprogressbar("destroy");
                            $("body").find('.tempo-alert-wrap').remove();
                            if(rs.success){
                                //获取生成案例id
                                var resourceId = rs.data.code;
                                for(var i = 0;i < builPermissionDatas.length;i++){
                                    //将id传递给授权信息中的resourceCode
                                    builPermissionDatas[i].resourceCode = resourceId;
                                }
                                if (builPermissionDatas.length == 0) {
                                    $("<div/>").alert({
                                        width: 500,
                                        height: 197,
                                        msg: "更新成功",
                                    });
                                }
                                if (builPermissionDatas.length > 0) {
                                    savePermission(builPermissionDatas, function(data){
                                        $("<div/>").alert({
                                            width: 500,
                                            height: 197,
                                            msg: "更新成功",
                                        });
                                    });
                                }
                                dialog.dialog("destroy").remove();
                                $(".tempo-alert").remove();
                                gridSelf.trigger("reloadGrid");
                            }else{
                                $("<div/>").alert({
                                    msg : rs.message,
                                    title : '提示'
                                });
                            }
                        },
                        error:function(){
                            pro.tempoprogressbar("destroy");
                            $(".tempo-alert").remove();
                            dialog.dialog("destroy").remove();
                        }
                    });
                }
            },{
                text : '取消',
                id : 'tempo-casebase-reset-btn',
                click : function() {
                    dialog.dialog("destroy").remove();
                }
            } ],
            close : function() {
                dialog.dialog("destroy").remove();	// 销毁弹出框,并返回
            }
        });
    };
    /**
     * 下一步 上一步事件
     * @type {number}
     */
    function ControlContent(index,dialog) {
        if(index == 1){
            dialog.find('#dialog-casebase-tempo').addClass('main-hide');
            dialog.find('#point1').addClass('c-select');
            dialog.find('#line0').addClass('b-select');
            dialog.find('.poetry1').css("color",'#337BFF');
            dialog.find('#tempo-mine-authorrization').css("display","block");
            dialog.find('#tooltip').css("display","block");
            dialog.find('.poetry2').css("color",'#337BFF');
            dialog.find('.tree-cell-title div').css('float','left').css('top','8px');
            // dialog.find('.tree-icon').css('margin-top','0.75em');
            dialog.find('#theme-tip').css("display","none");
        }else if(index == 0){
            dialog.find('#dialog-casebase-tempo').removeClass('main-hide');
            dialog.find('#point1').removeClass('c-select');
            dialog.find('#line0').removeClass('b-select');
            dialog.find('.poetry1').css("color",'#333');
            dialog.find('.poetry2').css("color",'#333');
            dialog.find('#tempo-mine-authorrization').css("display","none");
            dialog.find('#tooltip').css("display","none");
            // dialog.find('.tree-icon').css('margin-top','0')
            dialog.find('#theme-tip').css("display","block");
        }

    }

    /**
     * 案例权限保存
     * @param data
     * @param callback
     */
    var savePermission = function(data, callback){
        var that = this;
        var process = $("<div/>").tempoprogressbar();
        $.ajax({
            url:contextPath + '/api/ai/resource/permission/modify',
            data: JSON.stringify(data),
            type: 'post',
            contentType: 'application/json',
            complete: function(XMLHttpRequest, textStatus){
                process.tempoprogressbar("destroy");
            },
            success:function(resultData){
                if(resultData.success){
                    callback.call(that, resultData.data);
                }else{
                    $("<div/>").alert({
                        width: 500,
                        height: 197,
                        msg:resultData.message
                    });
                }
            }
        });
    }

    init();

});